package com.smart.business.controller;

import com.smart.aop.log.LogType;
import com.smart.aop.log.SaveLog;
import com.smart.aop.permission.HasPermission;
import com.smart.business.dao.ChannelDao;
import com.smart.common.annotation.PermissionCheck;
import com.smart.entity.business.ChannelEntity;
import com.smart.entity.business.CollectChannelVO;
import com.smart.entity.business.CollectorEntity;
import com.smart.model.response.r.Result;
import com.smart.model.response.r.ResultCode;
import com.smart.service.business.CollectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 采集器配置 Controller
 *
 * @author wf
 * @since 2025-07-14 16:30:22
 */
@RestController
@RequestMapping("/business/collector")
public class CollectorController {
    @Autowired
    CollectorService collectorService;
    @Autowired
    private ChannelDao channelDao;


    /**
     * 根据名称,采集器编号进行查询
     */
    @PermissionCheck
    @GetMapping("/getCollectorList")
    public String getCollectorList(
                               @RequestParam(required = false) String collectorName,
                               @RequestParam(required = false) String collectorNumber,
                               @RequestParam(required = false) String companyName,
                               @RequestParam(defaultValue = "1") int current,
                               @RequestParam(defaultValue = "10") int size
                               ) {
        List<CollectorEntity> list = collectorService.getCollectorList( collectorName, collectorNumber,current,size,companyName);
        return Result.data(list);
    }
    /**
     * 列表（不分页）
     *
     * @param collectorEntity 采集器配置bean
     * @return String
     */
    @GetMapping("/list")
    public String list(CollectorEntity collectorEntity) {
        return Result.data(collectorService.findList(collectorEntity));
    }

    /**
     * 信息
     *
     * @param id 主键ID
     * @return String
     */
    @GetMapping("/get")
    public String get(@RequestParam String id) {
        CollectorEntity result = collectorService.get(id);
        return result != null ? Result.data(result) : Result.fail(ResultCode.FAIL);
    }

    /**
     * 保存
     *
     * @param collectChannelVO 采集器配置bean
     * @return String
     */
    @HasPermission("collector:add")
    @PostMapping("/save")
    @SaveLog(module = "采集器配置管理" , type = LogType.ADD)
    @PermissionCheck
    public String save(@RequestBody CollectChannelVO collectChannelVO) {
        CollectChannelVO result = collectorService.saveCollector(collectChannelVO);
        return result != null ? Result.data(result) : Result.fail(ResultCode.FAIL);
    }

    /**
     * 修改
     *
     * @param collectChannelVO 采集器配置bean
     * @return String
     */
    @HasPermission("collector:update")
    @PostMapping("/update")
    @SaveLog(module = "采集器配置管理" , type = LogType.UPDATE)
    @PermissionCheck
    public String update(@RequestBody CollectChannelVO collectChannelVO) {
        CollectChannelVO result = collectorService.updateCollectorConfig(collectChannelVO);
        return result != null ? Result.data(result) : Result.fail(ResultCode.FAIL);
    }

    /**
     * 删除
     *
     * @param deleteIds 采集器配置bean
     * @return String
     */
    @HasPermission("collector:delete")
    @PostMapping("/delete")
    @SaveLog(module = "采集器配置管理" , type = LogType.UPDATE)
    @Transactional
    public String delete(@RequestBody List<String> deleteIds) {
        return Result.status(collectorService.deleteCollector(deleteIds));
    }


    /**
     * 获取通道参数
     * @param collectorId 采集器id
     * @return String
     */
    @GetMapping("/getChannelParamList")
    public String getChannelParamList(@RequestParam String collectorId) {
        List<ChannelEntity> list = collectorService.getChannelParamList(collectorId);
        return Result.data(list);
    }

    /**
     * 新增通道参数
     * @param channelEntity 通道参数bean
     * @return String
     */
    @PostMapping("/saveChannelParam")
    public String saveChannelParam(@RequestBody ChannelEntity channelEntity) {
        return Result.status(collectorService.saveChannelParam(channelEntity));
    }

    /**
     * 删除通道参数
     * @param channelEntity 通道参数bean
     * @return String
     */
    @PostMapping("/deleteChannelParam")
    public String deleteChannelParam(@RequestBody ChannelEntity channelEntity) {
       return Result.status(collectorService.deleteChannelParam(channelEntity));
    }



}
